package com.sun.module_base.ext.lifecycle

import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch

/**
 *@author:sunc
 *@time :2025/7/21 10:33
 *@desc: 描述
 */
class KtxAppLifeObserver: DefaultLifecycleObserver {
    private var _isFront = MutableStateFlow<Boolean>(false)
    var isFront = _isFront.asSharedFlow()

    /**
     * 在前台
     */
    override fun onStart(owner: LifecycleOwner) {
        super.onStart(owner)
        MainScope().launch { _isFront.emit(true) }
    }

    /**
     * 在后台
     */
    override fun onStop(owner: LifecycleOwner) {
        super.onStop(owner)
        MainScope().launch { _isFront.emit(false) }
    }
}